package org.zfes.snowier.cloud.zbss.cms.column.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.zfes.snowier.core.data.DataSet;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.zfes.snowier.core.util.ZBeanUtil;
import org.zfes.snowier.core.util.ZDateUtil;
import org.zfes.snowier.core.util.ZAlert;
import org.zfes.snowier.core.util.ZAssert;
import org.zfes.snowier.common.dao.params.ParamMap;
import org.zfes.snowier.cloud.zbss.cms.column.service.ICmsColumnService;
import org.zfes.snowier.cloud.zbss.cms.column.service.ICmsColumnUserService;
import org.zfes.snowier.cloud.zbss.cms.column.dao.CmsColumnUserMapper;
import org.zfes.snowier.cloud.zbss.cms.column.model.CmsColumnUser;

import java.util.stream.Collectors;
import java.util.stream.Stream;
@Service
public class CmsColumnUserServiceImpl implements ICmsColumnUserService{
 	@Autowired
	private CmsColumnUserMapper cmsColumnUserMapper;
 	@Autowired
 	private ICmsColumnService cmsColumnService;
	
	@Transactional(rollbackFor=Exception.class)
 	@Override
	public void createCmsColumnUser(Long userId,String columnIdMap) {
		ZAssert.notNull(userId, "请选择数据");
		ZAssert.notNull(columnIdMap, "请选择数据");
		ZAssert.hasText(columnIdMap, "请选择数据");
		
		//取出逗号分隔字符串中columnId
		List<Long> columnIdList=Arrays.asList(columnIdMap.split(",")).stream().map(s->Long.parseLong(s.trim())).collect(Collectors.toList());
		
		//查找出所有必须订阅的栏目列表
		List<Long> defaultColumnList=cmsColumnService.loadColumnIdByIsDefault(Byte.valueOf("1"));
		
		//未包含所有必须订阅的栏目
		if(!columnIdList.containsAll(defaultColumnList)) {
			ZAlert.serviceLogicalError("系统默认栏目必须全部选择");
		}
		
		//若包含所有必须订阅的栏目,则创建columnUser并初始化
		CmsColumnUser columnUser=new CmsColumnUser();
        ZBeanUtil.setCreateInfo(columnUser);   
        columnUser.setUserId(userId);
        
        //生成columnUserList
        List<CmsColumnUser> columnUserList = new ArrayList<CmsColumnUser>();
		for(int i=0;i<columnIdList.size();i++) {
			columnUser.setColumnId(columnIdList.get(i));
			columnUserList.add(columnUser);
		}
		//物理删除已有该用户订阅记录
		cmsColumnUserMapper.deleteByUserId(userId);
		
		//批量插入新记录
		cmsColumnUserMapper.insert(columnUser);
	}
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public Optional<CmsColumnUser> loadCmsColumnUserById(Long id) {
		if(id!=null){
			return Optional.ofNullable(cmsColumnUserMapper.selectByPrimaryKey(id));
		}
		return Optional.empty();
		
	}
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void updateCmsColumnUser(CmsColumnUser cmsColumnUser) {
		ZBeanUtil.validateBean(cmsColumnUser);
		cmsColumnUser.setUpdateTime(ZDateUtil.newDate());
		cmsColumnUserMapper.updateByPrimaryKeySelective(cmsColumnUser);
	}
	
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void deleteCmsColumnUser(Long id) {
		ZAssert.notNull(id, "请选择要操作的数据");
		cmsColumnUserMapper.deleteByPrimaryKey(id);
	}
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void setCmsColumnUserStatus(Long id, Byte status) {
		ZAssert.notNull(id, "请选择要操作的数据");
		ZAssert.notNull(status, "状态参数错误");
		if(!Stream.of(new Byte[] {-1,0,1}).anyMatch(val->val.equals(status))) {
			ZAlert.serviceLogicalError("状态参数错误");
		}
		cmsColumnUserMapper.updateStatus( id,  status) ;
	}
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public DataSet loadCmsColumnUserDataSet(Map<String, Object> params) {
		ParamMap pm=ParamMap.filterParam(params);
		DataSet ds=DataSet.newDS2(cmsColumnUserMapper.selectListCount(pm), cmsColumnUserMapper.selectMapListPage(pm));
		return ds;
	}
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void deleteByColumnId(Long columnId) {
		cmsColumnUserMapper.deleteByColumnId(columnId);
	}
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void deleteByUserId(Long userId) {
		cmsColumnUserMapper.deleteByUserId(userId);
		
	}
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public List<Long> loadColumnIdByUserId(Long userId) {
		ZAssert.notNull(userId, "userId不能为空");
		return cmsColumnUserMapper.loadColumnIdByUserId(userId);
	}
	
}